package ui.widget;

import util.Position;
import util.Translator;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;

public class GridArea {
	int x,y;
	int xSelected;
	int ySelected;
	Bitmap image;
	Bitmap selectedImage;
	Runnable runnable;
	EncodedImage encodedImage;

	public GridArea(Bitmap selectedImage, Runnable runnable) {
		this((Bitmap)null,selectedImage,runnable,0,0,0,0);
	}
	
	public GridArea(EncodedImage encodedImage, Bitmap selectedImage, Runnable runnable,int x,int y, int xSelected, int ySelected) {
		this((Bitmap)null,selectedImage,runnable,x,y,xSelected,ySelected);
		this.encodedImage = encodedImage;
	}
	
	public GridArea(Bitmap image, Bitmap selectedImage, Runnable runnable,int x,int y, int xSelected, int ySelected) {
		this.image = image;
		this.selectedImage = selectedImage;
		this.runnable = runnable;
		this.x=x;
		this.y=y;
		this.xSelected = xSelected;
		this.ySelected = ySelected;
	}
	
	public void paintSelection(Graphics graphics, Translator t) {
		if (selectedImage!=null) {
			Position translatedPos = t.translate(xSelected, ySelected);
			graphics.drawBitmap(translatedPos.x, translatedPos.y, selectedImage .getWidth(), selectedImage.getHeight(), selectedImage, 0, 0);
		}
	}
	
	public void paintImage(Graphics graphics, Translator t) {
		Position translatedPos = t.translate(x, y);
		if (image!=null) 
			graphics.drawBitmap(translatedPos.x, translatedPos.y, image .getWidth(), image.getHeight(), image, 0, 0);
		else if (encodedImage!=null) 
			graphics.drawImage(translatedPos.x, translatedPos.y, encodedImage.getWidth(), encodedImage.getHeight(), encodedImage, 0, 0, 0);
	}
	
	public XYRect getXYRect(Translator translator) {
		XYRect res;
		if (selectedImage != null) {
			Position xyTranslated = translator.translate(xSelected, ySelected);
			res = new XYRect(xyTranslated.x, xyTranslated.y, selectedImage.getWidth(), selectedImage.getHeight());
		}
		else if (image!=null){
			Position xyTranslated = translator.translate(x, y);
			res = new XYRect(xyTranslated.x, xyTranslated.y, image.getWidth(), image.getHeight());
		}
		else if (encodedImage!=null) {
			Position xyTranslated = translator.translate(x, y);
			res = new XYRect(xyTranslated.x, xyTranslated.y, encodedImage.getWidth(), encodedImage.getHeight());
		}
		else 
			res = new XYRect(0,0,Display.getWidth(),Display.getHeight());
		return res;
	}
	
	public Bitmap getSelectedImage() {
		return selectedImage;
	}

	public void setSelectedImage(Bitmap selectedImage) {
		this.selectedImage = selectedImage;
	}

	public Runnable getRunnable() {
		return runnable;
	}

	public void setRunnable(Runnable runnable) {
		this.runnable = runnable;
	}
}
